<?php

/**
 * Class MemcachedCacher
 */
class MemcachedCacher implements ICacher
{
    /**
     * @var
     */
    protected $live_time = CACHE_LIVE_TIME;
    /**
     * @var string
     */
    protected $host = 'localhost';
    /**
     * @var int
     */
    protected $port = 11211;

    /**
     *
     */
    public function __construct()
    {
        $this->database = new Memcached();
        $this->database->addServer($this->host, $this->port);
    }

    /**
     * @param $key
     * @return mixed
     */
    public function get($key)
    {
        return $this->database->get($key);
    }

    /**
     * @param $key
     * @param $value
     * @param int $time
     */
    public function set($key, $value, $time = 0)
    {
        $time = $time ?: $this->live_time;
        $this->database->set($key, $value, time() + $time);
    }
}
